package com.vmove.childtrain.utils;

import android.animation.AnimatorInflater;
import android.animation.ArgbEvaluator;
import android.animation.FloatEvaluator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EdgeEffect;
import android.widget.Toast;


import com.vmove.childtrain.common.AppContext;

import java.lang.reflect.Field;


public class UIUtils {

	public static Context getContext() {
		return AppContext.getInstance();
	}

	public static Thread getMainThread() {
		return AppContext.getMainThread();
	}

	public static long getMainThreadId() {
		return AppContext.getMainThreadId();
	}

	/** dip转换px */
	public static int dip2px(int dip) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (dip * scale + 0.5f);
	}

	/** px转换dip */
	public static int px2dip(int px) {
		final float scale = getContext().getResources().getDisplayMetrics().density;
		return (int) (px / scale + 0.5f);
	}

	public static float size2sp(float sp, Context context) {
		return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
				sp, context.getResources().getDisplayMetrics());
	}

	public static float getTextHeight(Paint textPaint) {
		return -textPaint.ascent() - textPaint.descent();
	}

	public static float calcTextSuitBaseY(RectF rectF, Paint paint) {
		return rectF.top + rectF.height() / 2 -
				(paint.getFontMetrics().ascent + paint.getFontMetrics().descent) / 2;
	}

	public  static int getCeil5(float num) {
		return ((int) (num + 4.9f)) / 5 * 5;
	}

	/**
	 * 为边界设置颜色效果
     */
	public   static void trySetColorForEdgeEffect(EdgeEffect edgeEffect, int color) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			edgeEffect.setColor(color);
			return;
		}
		try {
			Field edgeField = EdgeEffect.class.getDeclaredField("mEdge");
			edgeField.setAccessible(true);
			Drawable mEdge = (Drawable) edgeField.get(edgeEffect);
			mEdge.setColorFilter(color, PorterDuff.Mode.SRC_IN);
			mEdge.setCallback(null);
			Field glowField = EdgeEffect.class.getDeclaredField("mGlow");
			glowField.setAccessible(true);
			Drawable mGlow = (Drawable) glowField.get(edgeEffect);
			mGlow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
			mGlow.setCallback(null);
		} catch (Exception ignored) {
		}
	}

	/** 获取主线程的handler */
	public static Handler getHandler() {
		return AppContext.getMainThreadHandler();
	}

	/** 延时在主线程执行runnable */
	public static boolean postDelayed(Runnable runnable, long delayMillis) {
		return getHandler().postDelayed(runnable, delayMillis);
	}

	/** 在主线程执行runnable */
	public static boolean post(Runnable runnable) {
		return getHandler().post(runnable);
	}

	/** 从主线程looper里面移除runnable */
	public static void removeCallbacks(Runnable runnable) {
		getHandler().removeCallbacks(runnable);
	}

	public static View inflate(int resId){
		return LayoutInflater.from(getContext()).inflate(resId,null);
	}

	/**
	 * 加载有父容器的layout文件
     */
	public static View inflate(int resId, ViewGroup parent){
		return  LayoutInflater.from(getContext()).inflate(resId,parent,false);
	}

	/** 获取资源 */
	public static Resources getResources() {
		return getContext().getResources();
	}

	/** 获取文字 */
	public static String getString(int resId) {
		return getResources().getString(resId);
	}

	/** 获取文字数组 */
	public static String[] getStringArray(int resId) {
		return getResources().getStringArray(resId);
	}

	/** 获取dimen */
	public static int getDimens(int resId) {
		return getResources().getDimensionPixelSize(resId);
	}

	/** 获取drawable */
	public static Drawable getDrawable(int resId) {
		return getResources().getDrawable(resId);
	}

	/** 获取颜色 */
	public static int getColor(int resId) {
		return getResources().getColor(resId);
	}

	/** 获取颜色选择器 */
	public static ColorStateList getColorStateList(int resId) {
		return getResources().getColorStateList(resId);
	}
	//判断当前的线程是不是在主线程 
	public static boolean isRunInMainThread() {
		return android.os.Process.myTid() == getMainThreadId();
	}
    
	public static void runInMainThread(Runnable runnable) {
		if (isRunInMainThread()) {
			runnable.run();
		} else {
			post(runnable);
		}
	}

	/** 对toast的简易封装。线程安全，可以在非UI线程调用。 */
	public static void showToastSafe(final int resId) {
		showToastSafe(getString(resId));
	}

	/** 对toast的简易封装。线程安全，可以在非UI线程调用。 */
	public static void showToastSafe(final String str) {
		if (isRunInMainThread()) {
			showToast(str);
		} else {
			post(new Runnable() {
				@Override
				public void run() {
					showToast(str);
				}
			});
		}
	}
	
	private static void showToast(String str) {
		Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
	}


	public static ObjectAnimator GenerateColorAnimator(Context context, int animatorID, Object target) {
		ObjectAnimator colorAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(context, animatorID);
		colorAnimation.setTarget(target);
		colorAnimation.setEvaluator(new ArgbEvaluator());
		return colorAnimation;
	}

	public static ObjectAnimator GenerateSlideAnimator(Context context, int animatorID, Object target) {
		ObjectAnimator alphaAnimation = (ObjectAnimator) AnimatorInflater.loadAnimator(context, animatorID);
		alphaAnimation.setTarget(target);
		alphaAnimation.setEvaluator(new FloatEvaluator());
		return alphaAnimation;
	}
}


